package org.example.Contoller;

import org.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
@EnableWebMvc
@ResponseBody
public class UserController {
    @GetMapping
    public List<User> page(@RequestParam(required = false,defaultValue = "1")int page,
                           @RequestParam(required = false,defaultValue = "10") int size) {
        return null;
    }

    @PostMapping
    public User save(@RequestBody User user) {
        return user;
    }

    @GetMapping("{id}")
    public User detail(@PathVariable int id) {
        return null;
    }

    @PutMapping
    public User update(@RequestBody User user) {
        return user;
    }

    @DeleteMapping("{id}")
    public User delete(@PathVariable Integer id) {
        return null;
    }

    @GetMapping("search")
    public List<User> search(String keywork,@RequestParam(required = false) String name,
                             @RequestParam(required = false) Integer age) {
        return null;
    }
    @PostMapping("register")
    //使用校验注解 1.实体类属性添加校验注解  2.handler(@validated实体类 对象)
    //接收错误绑定信息，自定义返回结果
    //1.handler(校验对象,BindingResult result)  要求：BindingResult必须紧挨者校验对象
    //2.bindingresult获取绑定错误

    public Object register(@Validated @RequestBody User user, BindingResult result) {
        System.out.println("user"+user);
        if(result.hasErrors()) {
            Map data = new HashMap();
            data.put("code",400);
            data.put("msg","参数格式异常");
            return data;
        }else{
            return user;
        }
    }
}
